/**
* 运维工具  修改文件
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文件分发</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item>
        <el-select size="small" v-model="formInline.hostlist" placeholder="请选择主机" multiple>
          <el-option v-for="type in hostlist" :label="type.host_name_ip" :value="type.host_ip"
                     :key="type.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上传文件" prop="py_title">
        <input class="file" name="file" type="file" accept="image/png,image/gif,image/jpeg" ref="myfile"/>
      </el-form-item>
      <br>
      <el-form-item>
        <el-input size="small" v-model="formInline.path" placeholder="输入服务器的文件路径" style="width: 500px"></el-input>
      </el-form-item>
      <el-button size="small" type="primary" icon="el-icon-search" @click="pushFile(formInline)">分发</el-button>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="pullLinuxCommands(formInline)">指令操作</el-button>
      <br>
      <el-form-item>
        <el-select size="small" v-model="formInline.versionlist" placeholder="请选择回退版本" multiple>
          <el-option v-for="type in versionlist" :label="type.ver_name" :value="type.ver_name"
                     :key="type.id"></el-option>
        </el-select>
      </el-form-item>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="Fallback(formInline)">回退</el-button>


    </el-form>
    <!--  执行结果界面  -->
    <div id="editor">
      <el-input type="textarea" placeholder="请输入内容" v-model="formInline.textarea" show-word-limit rows=18>
      </el-input>
    </div>
  </div>
</template>

<script>
import {devHostList, getLinuxCommands, filesFork, varName, verBack, delay_Textarea} from '../../api/devMG'


export default {
  data() {
    return {
      hostlist: [
        {host_ip: '请选择', id: 0},
        {host_ip: '已处理', id: 1},
        {host_ip: '未处理', id: 2},
      ],
      versionlist: [
        {ver_name: '请选择', id: 0},
        {ver_name: '版本1', id: 1},
        {ver_name: '版本2', id: 2},
      ],
      formInline: {
        textarea: '',
        hostlist: '',
        path: '',
        versionlist: '',
        user_id: JSON.parse(localStorage.getItem('userdata'))['username_id'] * 1,
        token: localStorage.getItem('logintoken')
      },
      t: null,
    }
  },
  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      devHostList(parameter)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.hostlist = res.data
          }
        })
      varName(parameter)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.versionlist = res.data
          }
        })
    },
    // linux 指令操作
    pullLinuxCommands(formInline) {
      getLinuxCommands(formInline)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.formInline.textarea = res.file_info
          }
        })
    },
    // 文件分发
    pushFile(formInline) {
      this.loading = false
      let myfile = this.$refs.myfile.files[0]
      let fromdata = new FormData()
      fromdata.append('myfile', myfile)
      fromdata.append('hostlist', formInline.hostlist)
      fromdata.append('path', formInline.path)
      fromdata.append('user_id', JSON.parse(localStorage.getItem('userdata'))['username_id'] * 1)
      fromdata.append('token', formInline.token)

      filesFork(fromdata)
        .then(res => {
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.formInline.textarea += res.file_info

            // 起个定时任务，每隔 1s 向后端查询一下是否执行完成
            this.t = setInterval(() => {
              delay_Textarea()
                .then(res => {
                  if (res.code == 9999) {
                    clearInterval(this.t)
                    this.t = null
                  }else{
                    this.formInline.textarea += res.file_info
                  }
                })
            }, 2000)
          }
        })
    },
    // 版本回退
    Fallback(formInline) {
      verBack(formInline)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.formInline.textarea = res.file_info
          }
        })
    },
  },
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>

